/* The dialog box where users enter starting points for the wiki spider */

#include <QtGui>
#include <iostream>

#include "URLDialog.h"

urlDialog::urlDialog(QWidget *parent) : QDialog (parent)
{
    QPushButton *setUrlButton = new QPushButton(tr("&Add Url"));
    QPushButton *removeUrlButton = new  QPushButton(tr("&Remove Url"));
    QPushButton *editUrlButton = new QPushButton(tr("&Edit Url"));
    QPushButton *cancelButton = new QPushButton(tr("&Cancel changes"));
    QPushButton *saveButton = new QPushButton(tr("&Save changes"));
    urlList = new QListWidget;
    urlPrefix = new QLabel;

    connect(setUrlButton, SIGNAL(clicked()), this, SLOT(addUrl()));
    connect(removeUrlButton, SIGNAL(clicked()), this, SLOT(confirmDel()));
    connect(editUrlButton, SIGNAL(clicked()), this, SLOT(editUrl()));
    connect(cancelButton, SIGNAL(clicked()), this, SLOT(closeDialog()));
    connect(saveButton, SIGNAL(clicked()), this, SLOT(saveUrls()));

    QVBoxLayout *layoutV = new QVBoxLayout; // Later includes both the horizontal layouts
    QHBoxLayout *layoutHT = new QHBoxLayout; // Top horizontal layout
    QHBoxLayout *layoutHB = new QHBoxLayout; // Bottom horizontal layout
    layoutHT -> addWidget(setUrlButton);
    layoutHT -> addWidget(removeUrlButton);
    layoutHT -> addWidget(editUrlButton);
    layoutV -> addLayout(layoutHT);
    layoutV -> addWidget(urlList);
    layoutHB -> addWidget(saveButton);
    layoutHB -> addWidget(cancelButton);
    layoutV -> addLayout(layoutHB);
    setLayout(layoutV);

    setWindowTitle(tr("Set Urls"));
}

/* Prompts the user and adds the inputed url to the urlList */
void urlDialog::addUrl()
{
    bool ok;
    QString url = QInputDialog::getText(this, tr("Add Url"),
                                tr("http://en.wikipedia.org/wiki/"), QLineEdit::Normal,
                                "", &ok);
    if(ok && !url.isEmpty())
        urlList -> addItem(url);
}

/* Presents the user with a dialog to make sure they want to delete a url */
void urlDialog::confirmDel()
{
    QList<QListWidgetItem*> tmpList = urlList -> selectedItems();
    if (!(tmpList.isEmpty())) // If there's something selected
    {
        QMessageBox::StandardButton ans;
        ans = QMessageBox::question(this, tr("Confirmation to delete Url"),
                           tr("Do you <b>really</b> want to delete the Url <u>%1</u>?").arg(urlList->currentItem()->text()),
                           QMessageBox::Yes | QMessageBox::No);
        if(ans == QMessageBox::Yes)
            removeUrl();
    }
}

/* Opens a dialog for the user to edit the selected url */
void urlDialog::editUrl()
{
    bool ok;
    int row = urlList -> currentRow();
    if(row == -1)// Fixes crash when user hits 'edit url', with no url selected
    { 
        return;
    }
    QString selTxt;
    QListWidgetItem *selected = urlList -> item(row);
    selTxt = selected -> text();
    QString finalUrl = selTxt;
    QString tmpUrl = QInputDialog::getText(this, tr("Edit Url"),
                                tr("http://en.wikipedia.org/wiki/"), QLineEdit::Normal,
                                selTxt, &ok);
    if(!tmpUrl.isEmpty()) // As long as there is something inputed from the user
        finalUrl = tmpUrl;
    urlList -> editItem(selected);
    selected -> setText(finalUrl);
}

/* Removes selected url */
void urlDialog::removeUrl()
{
    urlList -> takeItem(urlList -> currentRow());
}

/* Closes the urlDialog */
void urlDialog::closeDialog()
{
    done(0);
}

/* Saves the urls to a file */
void urlDialog::saveUrls()
{
    // Need to add something that saves the list to a file
    done(0);
}
